import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/widgets/max_bottom_sheet.dart';

import 'package:ball_lion/server/api/user_api.dart';
import 'package:ball_lion/utils/toast_utils.dart';
import 'package:ball_lion/utils/user.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/base/wqs_button.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';

class TeamFocusButton extends StatelessWidget {
  const TeamFocusButton(
      {super.key,
      required this.sportsId,
      required this.id,
      required this.isFocus,
      this.onTap,
      this.teamName});

  const TeamFocusButton.soccer(
      {super.key,
      required this.id,
      required this.isFocus,
      this.onTap,
      this.teamName})
      : sportsId = 1;

  const TeamFocusButton.basket(
      {super.key,
      required this.id,
      required this.isFocus,
      this.onTap,
      this.teamName})
      : sportsId = 2;

  final int sportsId;
  final int id;
  final String? teamName;
  final bool isFocus;
  final ValueChanged<bool>? onTap;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 16),
      child: GestureDetector(
        onTap: () => User.needLogin(onFocus),
        child: Container(
          height: 24,
          width: 56,
          alignment: Alignment.center,
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(4),
              border: Border.all(
                  color:
                      !isFocus ? Colours.white : Colours.white.withOpacity(0.6),
                  width: 0.5)),
          child: QsText(!isFocus ? '关注' : '已关注',
              needCenter: true,
              color: !isFocus ? Colours.white : Colours.white.withOpacity(0.6),
              fontSize: 13),
        ),
      ),
    );
  }

  onFocus() async {
    int? result;
    if (isFocus) {
      result = await UserApi.unFocusTeam(id, sportsId);
      if (result == 200) {
        Toast.text("取消关注成功");
        onTap?.call(false);
      } else {
        Toast.text("取消关注失败");
      }
    } else {
      Utils.onEvent('sjpd_qdxq', params: {'sjpd_qdxq': '3'});
      result = await UserApi.focusTeam(id, sportsId);
      if (result == 200) {
        Toast.text("关注成功");
        onTap?.call(true);
        Get.bottomSheet(sheet());
      }
    }
  }

  Widget sheet() {
    return MaxBottomSheet(
        onClosing: () {},
        shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.vertical(top: Radius.circular(12))),
        clipBehavior: Clip.antiAlias,
        builder: (context) => Padding(
              padding: const EdgeInsets.fromLTRB(24, 16, 24, 0),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  const QsText("关注成功",
                      fontWeight: FontWeight.w500, fontSize: 16),
                  Container(height: 4),
                  QsText("已自动关注${teamName ?? "球队"}近期比赛", color: Colours.grey66),
                  Container(height: 16),
                  Image.asset(
                    Utils.getImgPath("team_focus_hint.png"),
                    fit: BoxFit.cover,
                  ),
                  Container(height: 16),
                  QsButton(
                    width: 248,
                    height: 40,
                    onPressed: navigator?.pop,
                    text: "知道了",
                    borderRadius: 37.5,
                    foregroundColor: Colours.white,
                    backgroundColor: Colours.main,
                    textStyle: const TextStyle(
                        fontSize: 16, fontWeight: FontWeight.w500),
                  ),
                  Container(height: 12)
                ],
              ),
            ));
  }
}
